﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DRHotel_v1.Classes
{
    class AccountCreationClass
    {
        public bool createAccount(string staffID, string username, string password, string usertype)
        {
            SqlConnection conn = null;
            SqlCommand cmd = null;
            string strSQL = "Insert INTO Account (staffID,username,password,userType) VALUES (@staffID, @username, @password, @userType)";
            bool creationStatus = false;
            try
            {
                conn = new SqlConnection("data source=localhost; initial catalog=DRHotelMngSys; integrated security=false; user id=sa; password=imsa;  ");
                conn.Open();
                cmd = new SqlCommand(strSQL, conn);
                cmd.Parameters.AddWithValue("@staffID", Int32.Parse(staffID));
                cmd.Parameters.AddWithValue("@username", username);
                cmd.Parameters.AddWithValue("@password", password);
                cmd.Parameters.AddWithValue("@userType", usertype);
                int rows = cmd.ExecuteNonQuery();
                if (rows >= 1)
                {
                    creationStatus = true;

                }
                else
                {
                    creationStatus = false;
                }
                //dr = cmd.ExecuteReader();


            }
            catch (SqlException sqlex)
            {
                MessageBox.Show(sqlex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {

                conn.Close();

            }
            return creationStatus;
        }

        public static ArrayList PopulateStaff()
        {
            SqlConnection conn = null;
            SqlCommand cmd = null;
            SqlDataReader dr = null;
            ArrayList staffDetails = new ArrayList();
            string strSQL = "Select * From Staff ";
            try
            {

                conn = new SqlConnection("data source=localhost; initial catalog=DRHotelMngSys; integrated security=false; user id=sa; password=imsa;  ");
                conn.Open();
                cmd = new SqlCommand(strSQL, conn);
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    StaffDetailClass sta = new StaffDetailClass(Convert.ToInt32(dr["staffID"]),
                                               dr["firstName"].ToString(),
                                               dr["lastName"].ToString()
                                               );

                    staffDetails.Add(sta);

                }

                dr.Close();

            }
            catch (SqlException sqlex)
            {
                MessageBox.Show(sqlex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return staffDetails;
        }
    }
}
